package com.cgtong.android.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cgtong.android.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DeletePopupWindow extends PopupWindow {

    @BindView(R.id.delete_txt)
    TextView mDeleteTxt;
    private int popupWidth;
    private int popupHeight;

    // 坐标的位置（x、y）
    private final int[] location = new int[2];
    // 弹窗子类项选中时的监听
    private OnClickListener mClickListener;
    // 定义弹窗子类项列表

    public void setClickListener(OnClickListener mItemClickListener) {
        this.mClickListener = mItemClickListener;
    }


    public DeletePopupWindow(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_delete, null);
        ButterKnife.bind(this, view);

        this.setContentView(view);
        this.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);

        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupHeight = view.getMeasuredHeight();
        popupWidth = view.getMeasuredWidth();
    }

    @OnClick(R.id.delete_txt)
    public void onViewClicked() {
        if (mClickListener != null) {
            dismiss();
            mClickListener.onClick();
        }
    }

    /**
     * 功能描述：弹窗子类项按钮监听事件
     */
    public interface OnClickListener {
        void onClick();
    }

    public void showPopupWindow(View v) {
        //获取需要在其上方显示的控件的位置信息
        v.getLocationOnScreen(location);
        //在控件上方显示
        showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
    }
}